import 'dart:developer';
import 'package:ble_plugin/emind_home_ble_plugin.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final EmindHomeBlePlugin bleConfigurationApi = EmindHomeBlePlugin();
  TextEditingController sn_text = TextEditingController();
  TextEditingController ssid_text = TextEditingController();
  TextEditingController pwd_text = TextEditingController();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    bool initResult =
        await bleConfigurationApi.initAndSetAppKey("ij8TYFxxz0L2OZKko9CY");
    log('initResult====>$initResult');
  }

  Future<bool> checkPermission() async {
    List<Permission> permissions = [
      Permission.nearbyWifiDevices,
      Permission.location,
      Permission.locationWhenInUse,
      Permission.locationAlways,
      Permission.bluetoothScan,
      Permission.bluetoothConnect,
    ];

    Map<Permission, PermissionStatus> statuses = await permissions.request();

    if (statuses[Permission.nearbyWifiDevices] == PermissionStatus.granted) {}

    if (statuses[Permission.bluetoothScan] == PermissionStatus.granted) {}
    if (statuses[Permission.bluetoothConnect] == PermissionStatus.granted) {}

    if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
      // Use location.
      if (statuses[Permission.location] == PermissionStatus.granted) {}
    }

    //

    openAppSettings();
    return false;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Container(
              // width: MediaQuery.of(context).size.width,
              // height: 200,
              // color: Colors.blue,
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('SN：'),
                      SizedBox(
                          width: MediaQuery.of(context).size.width - 100,
                          height: 50,
                          child: TextField(controller: sn_text)),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('SSID：'),
                      SizedBox(
                          width: MediaQuery.of(context).size.width - 100,
                          height: 50,
                          child: TextField(controller: ssid_text)),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('Password：'),
                      SizedBox(
                          width: MediaQuery.of(context).size.width - 100,
                          height: 50,
                          child: TextField(controller: pwd_text)),
                    ],
                  )
                ],
              ),
            ),
            SizedBox(height: 15),
            Expanded(
              child: ListView.builder(
                  padding: EdgeInsets.fromLTRB(8, 8, 8, 0),
                  itemCount: 10,
                  itemBuilder: (context, index) {
                    return Container(
                      width: MediaQuery.of(context).size.width,
                      height: 50,
                      color: Colors.white,
                      child: Column(
                        children: [Text('time'), Text('info')],
                      ),
                    );
                  }),
            ),
            SizedBox(height: 15),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TextButton(
                    onPressed: () {
                      initPlatformState();
                    },
                    child: Container(
                      width: 100,
                      height: 50,
                      color: Colors.blue,
                      child: Center(
                        child: Text(
                          '初始化',
                          style: TextStyle(color: Colors.white, fontSize: 18),
                        ),
                      ),
                    )),
                TextButton(
                    onPressed: () {},
                    child: Container(
                      width: 100,
                      height: 50,
                      color: Colors.blue,
                      child: Center(
                        child: Text(
                          '开始',
                          style: TextStyle(color: Colors.white, fontSize: 18),
                        ),
                      ),
                    )),
                TextButton(
                    onPressed: () {},
                    child: Container(
                      width: 100,
                      height: 50,
                      color: Colors.blue,
                      child: Center(
                        child: Text(
                          '结束',
                          style: TextStyle(color: Colors.white, fontSize: 18),
                        ),
                      ),
                    ))
              ],
            ),
            SizedBox(height: 15)
          ],
        ),
      ),
    );
  }
}
